<template>
  <view class="revenue-stats-container">
    <view class="header">
      <text class="title">营收统计</text>
    </view>
    
    <!-- 时间筛选 -->
    <view class="filter-section">
      <picker mode="date" :value="startDate" @change="onStartDateChange">
        <view class="picker">
          {{ startDate }}
        </view>
      </picker>
      <text class="divider">至</text>
      <picker mode="date" :value="endDate" @change="onEndDateChange">
        <view class="picker">
          {{ endDate }}
        </view>
      </picker>
      <button class="filter-btn" @click="fetchRevenueStats">查询</button>
    </view>
    
    <!-- 营收概览 -->
    <view class="overview-section">
      <view class="stat-card income">
        <text class="stat-label">总收入</text>
        <text class="stat-value">¥{{ revenueStats.totalIncome }}</text>
      </view>
      
      <view class="stat-card expense">
        <text class="stat-label">总支出</text>
        <text class="stat-value">¥{{ revenueStats.totalExpense }}</text>
      </view>
      
      <view class="stat-card profit">
        <text class="stat-label">净利润</text>
        <text class="stat-value">¥{{ revenueStats.netProfit }}</text>
      </view>
      
      <view class="stat-card growth">
        <text class="stat-label">增长率</text>
        <text class="stat-value">{{ revenueStats.growthRate }}%</text>
      </view>
    </view>
    
    <!-- 收入分类统计 -->
    <view class="income-section">
      <text class="section-title">收入分类统计</text>
      <view class="income-list">
        <view class="income-item" v-for="item in revenueStats.incomeCategories" :key="item.category">
          <view class="category-info">
            <text class="category-name">{{ item.category }}</text>
            <text class="category-amount">¥{{ item.amount }}</text>
          </view>
          <view class="progress-bar">
            <view class="progress-fill" :style="{ width: item.percentage + '%' }"></view>
          </view>
          <text class="percentage">{{ item.percentage }}%</text>
        </view>
      </view>
    </view>
    
    <!-- 支出分类统计 -->
    <view class="expense-section">
      <text class="section-title">支出分类统计</text>
      <view class="expense-list">
        <view class="expense-item" v-for="item in revenueStats.expenseCategories" :key="item.category">
          <view class="category-info">
            <text class="category-name">{{ item.category }}</text>
            <text class="category-amount">¥{{ item.amount }}</text>
          </view>
          <view class="progress-bar">
            <view class="progress-fill" :style="{ width: item.percentage + '%' }"></view>
          </view>
          <text class="percentage">{{ item.percentage }}%</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { statisticsApi } from '../../../src/services/statistics'

// 日期筛选
const startDate = ref('')
const endDate = ref('')

// 营收统计数据
const revenueStats = ref({
  totalIncome: 0,
  totalExpense: 0,
  netProfit: 0,
  growthRate: 0,
  incomeCategories: [] as any[],
  expenseCategories: [] as any[]
})

// 初始化日期
const initDates = () => {
  const now = new Date()
  const oneMonthAgo = new Date(now.getFullYear(), now.getMonth() - 1, now.getDate())
  
  startDate.value = formatDate(oneMonthAgo)
  endDate.value = formatDate(now)
}

// 格式化日期
const formatDate = (date: Date) => {
  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, '0')
  const day = String(date.getDate()).padStart(2, '0')
  return `${year}-${month}-${day}`
}

// 开始日期变更
const onStartDateChange = (e: any) => {
  startDate.value = e.detail.value
}

// 结束日期变更
const onEndDateChange = (e: any) => {
  endDate.value = e.detail.value
}

// 获取营收统计数据
const fetchRevenueStats = async () => {
  try {
    const params = {
      startDate: startDate.value,
      endDate: endDate.value
    }
    const response: any = await statisticsApi.getRevenueStats(params)
    if (response.code === 200) {
      revenueStats.value = response.data
    }
  } catch (error) {
    console.error('获取营收统计数据失败:', error)
    uni.showToast({
      title: '获取数据失败',
      icon: 'error'
    })
  }
}

onMounted(() => {
  initDates()
  fetchRevenueStats()
})
</script>

<style scoped>
.revenue-stats-container {
  background-color: #ffffff;
  min-height: 100vh;
  padding: 20rpx;
}

.header {
  text-align: center;
  margin-bottom: 40rpx;
  padding-top: 20rpx;
}

.title {
  font-size: 36rpx;
  font-weight: bold;
  color: #333333;
}

.filter-section {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: #f8f8f8;
  border-radius: 16rpx;
  padding: 20rpx;
  margin-bottom: 40rpx;
}

.picker {
  background-color: #ffffff;
  border-radius: 8rpx;
  padding: 10rpx 20rpx;
  font-size: 24rpx;
}

.divider {
  margin: 0 20rpx;
  color: #666666;
}

.filter-btn {
  background-color: #4B0082;
  color: white;
  border: none;
  border-radius: 8rpx;
  padding: 10rpx 20rpx;
  font-size: 24rpx;
}

.overview-section {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20rpx;
  margin-bottom: 40rpx;
}

.stat-card {
  background-color: #f8f8f8;
  border-radius: 16rpx;
  padding: 30rpx 20rpx;
  text-align: center;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
}

.stat-label {
  display: block;
  font-size: 24rpx;
  color: #666666;
  margin-bottom: 8rpx;
}

.stat-value {
  font-size: 32rpx;
  font-weight: bold;
}

.stat-card.income .stat-value {
  color: #4CAF50;
}

.stat-card.expense .stat-value {
  color: #F44336;
}

.stat-card.profit .stat-value {
  color: #2196F3;
}

.stat-card.growth .stat-value {
  color: #FF9800;
}

.section-title {
  display: block;
  font-size: 32rpx;
  font-weight: bold;
  color: #333333;
  margin-bottom: 20rpx;
}

.income-list, .expense-list {
  background-color: #f8f8f8;
  border-radius: 16rpx;
  padding: 20rpx;
  margin-bottom: 40rpx;
}

.income-item, .expense-item {
  margin-bottom: 20rpx;
}

.income-item:last-child, .expense-item:last-child {
  margin-bottom: 0;
}

.category-info {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10rpx;
}

.category-name {
  font-size: 26rpx;
  color: #333333;
}

.category-amount {
  font-size: 26rpx;
  font-weight: bold;
  color: #4B0082;
}

.progress-bar {
  height: 12rpx;
  background-color: #eeeeee;
  border-radius: 6rpx;
  overflow: hidden;
  margin-bottom: 10rpx;
}

.progress-fill {
  height: 100%;
  background-color: #4B0082;
  border-radius: 6rpx;
}

.percentage {
  font-size: 22rpx;
  color: #999999;
  text-align: right;
  display: block;
}
</style>